package vip.zslaa.editor.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/img")
public class ImgUpController {
    //上传图片的配置，用的是commons工具，文件存储路径为项目以外
    @PostMapping("/imgUpLoad")
    public MdResult imgUpLoad(@RequestParam(value = "editormd-image-file", required = false) MultipartFile file) {
        String rootPath = "E:/image/";
        String path = rootPath + file.getOriginalFilename();
        try {
            file.transferTo(new File(path));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new MdResult(1, "上传成功",file.getOriginalFilename());
    }

    //MdResult的返回类型，注意返回的格式success，message，url
    public class MdResult {
        public Integer success;

        public String message;

        public String url;

        public MdResult(Integer success, String message, String url) {
            this.success = success;
            this.message = message;
            this.url = url;
        }
    }
}